<?php
/**
 * 供应商车型申请模型
 *
 * PHP version 7
 *
 * @category  Business
 * @package   App\Models\Business
 * @author    Yisheng Zheng <zhengys@boqii.com>
 * @copyright 2016-2019 zhengys Co. All Rights Reserved.
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
 * @version   GIT:<git_id>
 * @link      http://shop.openapi.boqii.com
 */
namespace App\Models\Supplier\SupplierMatorApply;

use App\Models\Model;
use Cache;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use App\Models\Mator;

class SupplierMatorApply extends Model
{
    /**
     * 供应商表
     *
     * @var string
     */
    protected $table = 'e_supplier_mator_apply';
	
	protected static $_instance = null;
	
	/**
	 * 单例模式
	 *
     * @access public
     * @return mixed
     */
	public static function getInstance()
	{
		if (self::$_instance === null) {
            self::$_instance = new self();
        }
        return self::$_instance;
	}
	
	/**
	 * 供应车型库申请列表
	 *
	 * @param Request $request
     * @access public
     * @return mixed
     */
	public function getSupplierApplyList($request)
	{
		$page = isset($request['Page']) ? $request['Page'] : 1;
        $pageSize = isset($request['PageSize']) ? $request['PageSize']:Config::get('app.page_size');
		$order_field = isset($request['OrderField']) ? $request['OrderField'] : 'create_time';
		$order_style = isset($request['OrderStyle']) ? $request['OrderStyle'] : 'DESC';
		$export= isset($request['Export']) ? true : false ;
        $offset = ($page-1) * $pageSize;
        $where = array();
		$where[] = ['is_valid','=',0];
		if(isset($request['SupplierId']) && !empty($request['SupplierId'])){
			$where[] = ['supplier_id','=',$request['SupplierId']];
		}
		$count = $this->where($where)->count();
		$result = $this->where($where)
					   ->orderBy($order_field,$order_style)
					   ->skip($export ? 0 : $offset)
					   ->take($export ? $count : $pageSize)
					   ->get()->toArray();
		$ret = array();
		$status_arr = array('待审核','通过');
		$mator_obj = new Mator();
		$where = array();
		$where['rela_status'] = 0;
		$field_country = $mator_obj->getFeildCountryWhere($where);
		$field_country_data = array();
		foreach($field_country as $f_c){
			$field_country_data[$f_c['id']] = $f_c;
		}
		foreach($result as $_r){
			$data = array();
			$data['FieldCountryId'] = $_r['f_c_id'];
			$data['FieldId'] = $field_country_data[$_r['f_c_id']]['field_id'];
			$where = array();
			$where['id'] = $data['FieldId'];
			$field = $mator_obj->getFeildWhere($where);
			$data['FieldName'] = $field[0]['field_name'];
			$data['CountryId'] = $field_country_data[$_r['f_c_id']]['country_id'];
			$where = array();
			$where['id'] = $data['CountryId'];
			$country = $mator_obj->getCountryWhere($where);
			$data['CountryName'] = $country[0]['country_name'];
			$data['MakeId'] = $_r['make_id'];
			$where = array();
			$where['id'] = $data['MakeId'];
			$make = $mator_obj->getMakeWhere($where);
			$data['MakeName'] = $make[0]['make_name'];
			$data['SeriesId'] = $_r['series_id'];
			$where = array();
			$where['id'] = $data['SeriesId'];
			$series = $mator_obj->getSeriesWhere($where);
			$data['SeriesName'] = $series[0]['series_name'];
			$data['MatorCode'] = $_r['mator_code'];
			$data['MatorName'] = $_r['mator_name'];
			$data['Engine'] = $_r['engine'];
			$data['Volume'] = $_r['volume'];
			$data['Capacity'] = $_r['capacity'];
			$data['HorsePower'] = $_r['horse_power'];
			$data['Mtype'] = $_r['m_type'];
			$data['Myear'] = $_r['m_year'];
			$data['StatusId'] = $_r['check_status'];
			$data['StatusName'] = $status_arr[$_r['check_status']];
			array_push($ret,$data);
		}
		return ['List'=>$ret,'Total'=>$count];
	}
}
